package com.pumpkin.jschsftpdemo.controller;

import com.pumpkin.jschsftpdemo.common.Result;
import com.pumpkin.jschsftpdemo.util.FTPUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/uploadFileServer")
public class UploadFileServerController {

    /**
     * 上传操作
     * @param file
     * @return
     */
    @PostMapping("/uploadFolder")
    public Result uploadFolder(MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            FTPUtil.sshSftp(bytes,file.getOriginalFilename());

            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }

    @GetMapping(value = "/getFile",produces = MediaType.IMAGE_PNG_VALUE)
    public void getFile(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        try {
            FTPUtil.getFile(fileName,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
